<template>
  <!--字典管理-->
  <div class="app-container">
    <el-form class="jc_search_wrap" v-model="searchList">
      <label>表名</label><el-input v-model="searchList.tableName"></el-input>
      <el-button @click="searchRows"><i class="el-icon-search"></i>查询</el-button>
    </el-form>
    <el-button-group class="jc_btn_wrap jc_btn_left">
      <el-button @click="createRow"><i class="jc_icon el-icon-plus"></i>添加字典主题</el-button>
      <el-button @click="editRow"><i class=""></i>编辑</el-button>
      <el-button @click="deleteRow"><i class=""></i>删除</el-button>
    </el-button-group>
    <el-table :height="tableHeight" :data="list" v-loading.body="listLoading" element-loading-text="拼命加载中" border fit highlight-current-row class="jc_table_wrap"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="80px">
      </el-table-column>
      <el-table-column label='字典id'>
        <template scope="scope">
          {{scope.row.dictId}}
        </template>
      </el-table-column>
      <el-table-column label='键'>
        <template scope="scope">
          {{scope.row.keyy}}
        </template>
      </el-table-column>
      <el-table-column label='值'>
        <template scope="scope">
          {{scope.row.valuee}}
        </template>
      </el-table-column>
      <el-table-column label='表名'>
        <template scope="scope">
          {{scope.row.tableName}}
        </template>
      </el-table-column>
      <el-table-column label='字段名'>
        <template scope="scope">
          {{scope.row.fieldName}}
        </template>
      </el-table-column>
    </el-table>
    <!--<el-pagination layout="prev, pager, next" :total="total" @current-change="changePage" :page-size="10" :current-page="currentPage"/>-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="changePage"
      :current-page="currentPage"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <!--新建-->
    <el-dialog title="新建角色" :visible.sync="create_visible" class="jc_dialog_wrap">
      <el-form :model="form_create" label-width="120px">
        <el-form-item label="键">
          <el-input v-model="form_create.keyy"></el-input>
        </el-form-item>
        <el-form-item label="值">
          <el-input v-model="form_create.valuee"></el-input>
        </el-form-item>
        <el-form-item label="表名">
          <el-input v-model="form_create.tableName"></el-input>
        </el-form-item>
        <el-form-item label="字段">
          <el-input v-model="form_create.fieldName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitCreateForm">确定</el-button>
        <el-button @click="create_visible=false">取消</el-button>
      </div>
    </el-dialog>
    <!--编辑-->
    <el-dialog title="编辑角色" :visible.sync="edit_visible" class="jc_dialog_wrap">
      <el-form :model="form_edit" label-width="120px">
        <el-form-item label="键">
          <el-input v-model="form_edit.keyy"></el-input>
        </el-form-item>
        <el-form-item label="值">
          <el-input v-model="form_edit.valuee"></el-input>
        </el-form-item>
        <el-form-item label="表名">
          <el-input v-model="form_edit.tableName"></el-input>
        </el-form-item>
        <el-form-item label="字段">
          <el-input v-model="form_edit.fieldName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitEditForm">确定</el-button>
        <el-button @click="edit_visible=false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

  import * as Dict from "../../api/systemApi/dictionary";

  export default {
    components: {

    },
    data() {
      return {
        searchList: {pageSize: '',},
        list: null,
        total: 0,
        create_visible: false,
        detail_visible: false,
        edit_visible: false,
        form_create: {},
        form_detail: {},
        form_edit: {},
        multipleSelection: [],
        pageSize: 10,
        currentPage: 1,
      }

    },
    created() {
      this.fetchData()
    },
    methods: {
      fetchData(p=1) {
        this.listLoading = true;
        this.searchList.pageNum = this.currentPage = p;
        this.searchList.pageSize = this.pageSize;
        Dict.getDictList(this.searchList).then(data => {
          this.list = data.rows;
          this.total = data.total;
          this.listLoading = false;
        })

      },
      searchRows() {
        this.fetchData()
      },
      emptyInputs() {
        this.searchList = {};
        this.fetchData()
      },
      createRow() {
        this.create_visible = true;
        this.form_create = {}
      },
      submitCreateForm() {
        Dict.createDict(this.form_create).then(data => {
          this.create_visible = false;
          this.fetchData();
        })
      },
      editRow() {
        if(this.multipleSelection.length === 0) {
          alert('请先勾选行');
        }else if(this.multipleSelection.length >= 2) {
          alert('每次最多勾选一行');
        }else if(this.multipleSelection.length === 1){
          Dict.detailRow({dictId:this.multipleSelection[0].dictId}).then(data => {
            this.edit_visible = true;
            this.form_edit = data.msg;
          })
        }
      },
      submitEditForm() {
        Dict.updateDict(this.form_edit).then(data => {
          this.edit_visible = false;
          this.fetchData(this.currentPage);
        })
      },
      detailRow(row) {},
      deleteRow() {
        if(this.multipleSelection.length === 0) {
          alert('请先勾选行');
        }else if(this.multipleSelection.length >= 2) {
          alert('每次最多勾选一行');
        }else if(this.multipleSelection.length === 1){
          this.$confirm('是否删除该条数据?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            Dict.delRow({ids:this.multipleSelection[0].dictId}).then(data => {
             this.fetchData(this.currentPage);
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
             })

          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            });
          });

        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      changePage(p) {
        this.fetchData(p)
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pageSize = val;
        this.currentPage = 1;
        this.fetchData(1);

      },
    },
    computed: {
      tableHeight() {
        const filterHeight = 28
        var tableHeight = this.getTableHeight(filterHeight)
        return tableHeight;
      }
    }
  }
</script>
